#import "MainView.h"
#include <sys/time.h>
#include "draw.h"
#include "App.h"
#import <CoreSurface/CoreSurface.h>
#import <UIKit/UIKit.h>
#import <UIKit/UISegmentedControl.h>
#import <UIKit/UISwitchControl.h>
#import <UIKit/UIPreferencesTableCell.h>
#import <CoreGraphics/CGContext.h>

#include <errno.h>

int __screenOrientation;
int __x1;
int __y1;
int __x2;
int __y2;


extern char emuThread;
static MainView *sharedInstance = nil;
void updateScreen() {

	[sharedInstance performSelectorOnMainThread:@selector(updateScreen) withObject:nil waitUntilDone: NO];
}

extern CGPoint GSEventGetInnerMostPathPosition(struct __GSEvent*);
extern CGPoint GSEventGetOuterMostPathPosition(struct __GSEvent*);


@implementation MainView 
- (id)initWithFrame:(struct CGRect)rect andParent:(id)parent
{
	mParent = parent;
    if ((self == [super initWithFrame: rect]) != nil) {
		[super setTapDelegate: self];
        sharedInstance = self;
        initGraphics = 0;
        __screenOrientation = [UIHardware deviceOrientation: YES];
		__x1=-1;
		__y1=-1;
		__x2=-1;
		__y2=-1;
 	[self setOpaque:YES];
  }
    return self;
}

- (void)dealloc {
        [ screenLayer release ];
        pthread_mutex_destroy(&screenUpdateMutex);
        pthread_cond_destroy(&screenUpdateLock);
	[super dealloc];
}

- (void)updateScreen {

	[sharedInstance setNeedsDisplay];
}

- (BOOL)ignoresMouseEvents {

	return NO;
}


- (void)mouseDragged:(GSEvent *)event {
    CGPoint p1 = GSEventGetInnerMostPathPosition(event);
    CGPoint p2 = GSEventGetOuterMostPathPosition(event);
	__x1 = p1.x;
	__y1 = p1.y;
	__x2 = p2.x;
	__y2 = p2.y;
}

- (void)mouseDown:(GSEvent *)event {
 	[self mouseDragged:event];
}

- (void)view:(UIView *)view handleTapWithCount:(int)count event:(GSEvent *)event {
	if (count ==2){
		[mParent Stop];
	}
}

- (void)mouseUp:(GSEvent *)event {
	__x1=-1;
	__y1=-1;
	__x2=-1;
	__y2=-1;
}

- (void)startEmulator {
	__x1=-1;
	__y1=-1;
	__x2=-1;
	__y2=-1;
	emuThread = 1;
	pthread_create(&emulation_tid, NULL, emulation_thread, NULL);
}

- (void)stopEmulator {
        emuThread = 0;
        pthread_join(emulation_tid, NULL);
		__x1=-1;
		__y1=-1;
		__x2=-1;
		__y2=-1;
}

- (void)doinitImage:(CGImageRef)image andClean:(bool)clean{
	if (initGraphics == 1){
		prepareImage(image);;
		if (clean) CFRelease(image);
	}
	else{
		initImage=0;
		sourceImage = image;
		cleanSourceImage = clean;
	}
}


- (void)drawRect:(CGRect)rect{
    if (initGraphics == 0) {
        initGraphics = 1;
        pthread_cond_init(&screenUpdateLock, NULL);
        pthread_mutex_init(&screenUpdateMutex, NULL);

        CoreSurfaceBufferLock(screenSurface, 3);

        screenLayer = [[LKLayer layer] retain];
        [screenLayer setFrame: CGRectMake(0.0f, 0.0f, 320.0f, 479.0f)];
		
        [screenLayer setContents: screenSurface];
        [screenLayer setOpaque: YES];
        [[self _layer] addSublayer: screenLayer];
        CoreSurfaceBufferUnlock(screenSurface);
		m_cba = CoreSurfaceBufferGetBaseAddress(screenSurface);
    }
    if (initImage == 0) {
		initImage = 1;
		prepareImage(sourceImage);;
		if (cleanSourceImage) CFRelease(sourceImage);
	}
}


@end
